package com.allwees.bs.module.oms.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.core.dynamic.annotation.MultiDynamicInject;
import com.allwees.bs.module.oms.dto.OrderRefundDto;
import com.allwees.bs.module.oms.entity.OrderRefund;
import com.allwees.bs.module.oms.req.query.OrderRefundQueryCondition;
import com.allwees.bs.module.oms.req.query.OrderRefundQueryReq;
import com.allwees.bs.module.oms.service.GeneralOrderRefundService;
import com.allwees.bs.module.oms.vo.ExportRefundVo;
import com.allwees.bs.module.pms.vo.ExportGaSkuVo;
import com.allwees.core.common.util.DateUtil;
import com.allwees.core.common.util.excel.ExcelUtil;
import com.allwees.core.common.util.http.RequestContext;
import me.zhengjie.utils.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 供应商 订单 退货
 *
 * @author mouhaining
 * @since 2020-03-02 15:53
 */
@RestController
@RequestMapping("/backstage/api/oms/order_refund")
public class GeneralOrderRefundApi extends BaseOrderRefundApi<GeneralOrderRefundService> {

    /**
     * 分页
     *
     * @param page
     * @param req
     * @return
     */
    @MultiDynamicInject(value = {@DynamicInject(type = DynamicInject.Type.Retailer, key = "retailerUuid"), @DynamicInject(key = "orderItemUuid")})
    @RequestMapping("/page")
    public R page(Page<OrderRefund> page, OrderRefundQueryReq req) {
        if (StringUtils.isEmpty(req.getUserUuid())) {
            req.setUserUuid(null);
        }
        if (StringUtils.isEmpty(req.getProductUuid())) {
            req.setProductUuid(null);
        }
        if (StringUtils.isEmpty(req.getOrderItemUuid())) {
            req.setOrderItemUuid(null);
        }
        if (StringUtils.isEmpty(req.getDefNo())) {
            req.setDefNo(null);
        }
        if (EBranchType.RETAILER.equals(getCurrentUser().getBranchType())) {
            req.setRetailerUuid(getCurrentUser().getRetailerUuid());
        }

        QueryWrapper<OrderRefund> wrapper = new QueryWrapper<>();
        req.buildQuery(wrapper);
        IPage<OrderRefundDto> page1 = boService.page(page, req);
        return R.ok(page1);
    }

    /**
     * 退款列表查询条件
     *
     * @return
     */
    @GetMapping("/get_refund_query_condition")
    public R getOrderRefundQueryCondition() {
        ResourceBundle resourceBundle = RequestContext.getContext().getResourceBundle();

        OrderRefundQueryCondition orderRefundQueryCondition = new OrderRefundQueryCondition();
        Map<String, String> map = new HashMap<>();
        map.put(resourceBundle.getString("refundStatus.REFUNDED"), "REFUNDED");
        map.put(resourceBundle.getString("refundStatus.REFUNDING"), "REFUNDING");
        map.put(resourceBundle.getString("refundStatus.REFUND_FAILED"), "REFUND_FAILED");
        map.put(resourceBundle.getString("refundStatus.REFUND_CANCELED"), "REFUND_CANCELED");
        orderRefundQueryCondition.setRefundStateList(map);
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put(resourceBundle.getString("refundStatus.UN_SHIPPING_PHASE"), "UN_SHIPPING_PHASE");
        hashMap.put(resourceBundle.getString("refundStatus.AFTER_SHIPPING_PHASE"), "AFTER_SHIPPING_PHASE");


        orderRefundQueryCondition.setRefundPhaseList(hashMap);
        //List<Store> list =  boService.findAllStoreList();
        //orderRefundQueryCondition.setStoreList(list);
        return R.ok(orderRefundQueryCondition);
    }

    /**
     * 批量导出退款订单
     *
     * @param req
     * @param response
     * @return
     */
    @GetMapping("/export_refund_list")
    public R exportRefundList(OrderRefundQueryReq req, HttpServletResponse response) {
        //req.setUserUuid(getCurrentUserUuid());
        QueryWrapper<OrderRefund> wrapper = new QueryWrapper<>();
        req.buildQuery(wrapper);
        List<ExportRefundVo> beans = boService.exportRefundList(req);
        String fileName = "退货列表" + "_" + DateUtil.formatDate(new Date()) + ".xlsx";
        ExcelUtil.writeExcel(response, beans, fileName, "退货列表", ExportGaSkuVo.class);
        return null;
    }

    @GetMapping("/{orderRefundUuid}/detail")
    public R detail(@PathVariable String orderRefundUuid) {
        Assert.isTrue(org.apache.commons.lang3.StringUtils.isNotEmpty(orderRefundUuid), "参数为空");
        OrderRefund orderRefund = boService.findByUuid(orderRefundUuid);
        return R.ok(orderRefund);
    }

}
